var Enemy = cc.Sprite.extend({
    _rect:null,
    _HP : 0,
    _OHP : 0,
    _speed : 0,
    _gold : 0,
    _life : true,
    _directType : 0,
    _xSpeed : 0,
    _ySpeed : 0,
    //////////////
    _bloodB : null,
    _bloodBar : null,
    _hitEffect : null,
    //////////////
    _animateRes : null,
    ///////////////////////////////////////////////////////////
    ctor : function(filename,direct,hp,speed,gold){
        this._super(filename);
        this._OHP = this._HP = hp;
        this._speed = speed;
        this._gold = gold;
        if(direct==1||direct==-1) {
            this._xSpeed = direct*this._speed;
            this.setScaleX( direct );
        }else if(direct==2||direct==-2) {
            this._ySpeed = direct*this._speed*0.5;
        }
        this._animateRes = new Array();
    },
    onEnter:function () {
        this._super();
        this.addBloodBar();
        this.addHitEffect();
    },
    onExit : function(){
        this._animateRes.splice(0,this._animateRes.length);
        this._super();
    },
    setAnimateRes:function(res){
        for(var i=0;i<res.length;i++){
            var name = res[i];
            this._animateRes.push(name);
        }
    },
    playAnimate : function(width,height,speed){
        var frames = [];
        for(var i=0;i<this._animateRes.length;i++){
            var name = this._animateRes[i];
            var filename = MainResource(name);
            var frame = cc.SpriteFrame.create( filename,cc.rect(0,0,width,height ) );
            frames.push( frame );
        }
        var animation = cc.Animation.create( frames, 0.3/(speed*0.01) );
        this.runAction(  cc.RepeatForever.create(
            cc.Animate.create( animation )
        ) );
    },
    ////////////////////////////////////////////////
    addHitEffect : function() {
        this._hitEffect = EnemyHurt.create("bdtx");
        this._hitEffect.setPosition( cc.p( this.width*0.5,this.height*0.5 ) );
        this._hitEffect.addMovementEventCallback(null);
        this._hitEffect.setVisible(false);
        this.addChild( this._hitEffect  );
    },
    playHitEffect : function(){
        if ( !this._hitEffect.isVisible() ){
            this._hitEffect.setVisible(true);
        }
        this._hitEffect.playAction(0);
    },
    ////////////////////////////////////////////////////////////////////////
    addBloodBar : function(){
        var filename = MainResource("blood0.png");
        this._bloodB = new cc.Sprite( filename  );
        this._bloodB.setPosition( cc.p( this.width*0.25,this.height + 5 ) );
        this._bloodB.setAnchorPoint( cc.p(0,0.5) );
        this._bloodB.setVisible(false);
        this.addChild( this._bloodB  );
        filename = MainResource("blood1.png");
        this._bloodBar = new cc.Sprite( filename );
        this._bloodBar.setPosition( cc.p( this.width*0.25,this.height + 5 ) );
        this._bloodBar.setAnchorPoint( cc.p(0,0.5) );
        this._bloodBar.setVisible(false);
        this.addChild( this._bloodBar  );
    },
    updateBloodBar : function(){
        if(!this._bloodBar.isVisible()){
            this.visibleBloodBar(true);
            this.schedule(this.visibleSchedule,3);
        }
        this._bloodBar.setScaleX( this._HP/this._OHP );
    },
    visibleSchedule:function(dt){
        this.visibleBloodBar(false);
    },
    visibleBloodBar:function(visible){
        if(visible){
            this._bloodBar.setVisible(true);
            this._bloodB.setVisible(true);
        }else{
            this._bloodBar.setVisible(false);
            this._bloodB.setVisible(false);
        }
    },
    ////////////////////////////////////////////////////////////////////////
    ////////////////////////// moving //////////////////////////
    moving : function( dt ,i ){
        this.x+= this._xSpeed*dt;
        this.y+= this._ySpeed*dt;
        var deadLine = 70;
        if( this.x < -deadLine || this.y < -deadLine ||
            this.y > cc.winSize.height + deadLine || this.x > cc.winSize.width + deadLine){
            this.dead(i);
        }
    },
    dead : function(){
        this._life = false;
        this.removeFromParent(true);
        //EnemiesDead(this,i); //turn to checking in loop and remove
    },
    hurt : function(hurt){
        this._HP-=hurt;
        this.playHitEffect();

        var index = GetRandom(0,4);
        if(index<0||index>4)index = 2;
        var filename = AudioResource("aene"+index+".mp3");
        cc.audioEngine.playEffect(filename);

        if( this._HP <= 0 ){
            // get it's gold to my money packet
            var iu = getUILayerInstance();
            iu.addMoney( this._gold );

            this.dead();
            return true;
        }else{
            this.updateBloodBar();
        }
        return false;
    },
    direct : function(type){
        if ( type == 1 || type == -1  ){
            this._ySpeed = this._speed*type;
            this._xSpeed = 0;
        }else if( type == 2 || type == -2 ){
            this._ySpeed = this._speed*type*0.5;
            this._xSpeed = 0;
        }else if( type == 3 || type == -3 ){
            var factor = type/3;
            this._xSpeed = this._speed*factor;
            this._ySpeed = 0;
            // change direct
            this.setScaleX( type>0?1:-1 );
            /*this.setScaleX( this.getScaleX()<0?
                this.getScaleX()*( factor<0?factor:-factor ):this.getScaleX()*factor );//*/
        }else if( type == 4 || type == -4 ){
            var factor = type*0.25;
            this._xSpeed = this._speed*factor;
            this._ySpeed = 0;
            // change direct
            this.setScaleX( type>0?1:-1 );
            /*this.setScaleX( this.getScaleX()<0?
                this.getScaleX()*( factor<0?factor:-factor ):this.getScaleX()*factor );//*/
        }
        this.setLocalZOrder(cc.winSize.height-this.y+this.getLocalZOrder());
    },
    collideRect:function (x, y) {
        var width=70,height=40;
        return cc.rect(x-width*0.5, y-height*0.5, width, height);
    }
});
////////////////////////////////////////////////////////////////
Enemy.create = function (filename,direct,hp,speed,gold) {
    var enemy = new Enemy(filename,direct,hp,speed,gold);
    //put  into our contain
    return enemy;
};

